// pages/service/sellCar.js
var $api = require('../../utils/api');
var util = require('../../utils/util');
var area_standard = require('../../utils/areaStandard');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_ver: 1,
    baojia_id:0,
    baojia: [],
    offer_vip_time_status: 1,
    publish_id: 1,
    name:'',
    phone: '',
    userInfo:[],
    autosize: {
      maxHeight: 50,
      minHeight: 50
    }, //输入框高度
    valid_time: [{
        text: '3小时',
      },{
        text: '6小时',
      },{
        text: '24小时',
      },
    ], 
    valid_timePopup:false,
    // 提交数据
    form_price: "",
    form_first_price: "",
    form_valid_time: "",
    form_area: "",
    form_remark: "",
  },


  //打开位置选择器
  openCityPopup: function (e) {
    this.setData({
      cityColumns: area_standard.default,
      showCityPopup: true
    })
  },
  onCloseCityPopup: function (e) {
    this.setData({
      showCityPopup: false
    })
  },
  onChangeCityPopup: function (e) {
    let list = e.detail.values;
    //拼接后端接受的区域格式
    let region = list[0].name + "/" + list[1].name + "/" + list[2].name;
    this.setData({
      form_area: region,
      showCityPopup: false
    })
  },

  //打开有效期选择器
  valid_timePopup: function (e) {
    this.setData({
      valid_timePopup: true
    })
  },
  //关闭有效期选择器
  onCloseValid_timePopup: function (e) {
    this.setData({
      valid_timePopup: false
    })
  },
  onChangeValid_time: function (e) {
    this.setData({
      form_valid_time: e.detail.value.text,
      valid_timePopup: false
    })
  },

  //发布车源
  release: function () {
    console.log(this.data);
    if (this.data.publish_id != "" && this.data.form_price != "" && this.data.form_first_price != "" &&
      this.data.form_valid_time != "" && this.data.form_area != "" ) 
    {
      let params = {
        publish_id: this.data.publish_id,
        price: this.data.form_price,
        first_price: this.data.form_first_price,
        valid_time: this.data.form_valid_time,
        area: this.data.form_area,
        remark: this.data.form_remark,
      }
      $api.add_new_car_publish_offer(params).then(res => {
        if (res.code == 1) {
          wx.showToast({
            title: '提交成功!',
            icon: 'none',
            duration: 2000 //持续的时间
          })
          wx.navigateBack({
            delta: 1,
          })
        }
        else
        {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      })
    } else {
      wx.showToast({
        title: '请填写完整信息后提交!',
        icon: 'none',
        duration: 2000 //持续的时间
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  onLoad: function (options) {
    this.setData({
      baojia_id: options.baojia_id,
      publish_id: options.publish_id,
   })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  console.log(this.data)
    if(this.data.baojia_id)
    {
      let param = {
        baojia_id: this.data.baojia_id,
      }
      $api.getBaojiaDetail(param).then(res => {
        if (res.code == 1) {
          this.setData({
            form_price: res.data.price,
            form_first_price: res.data.first_price,
            form_valid_time: res.data.valid_time,
            form_area: res.data.area,
            name: res.data.user.username,
            phone: res.data.user.mobile,
            form_remark: res.data.remark,
            userInfo:res.data
          })
        }
      })
    }
    else
    {
      let param;
      $api.userInfo(param).then(res => {
        if (res.code == 1) {
          this.setData({
            name: res.data.username,
            phone: res.data.mobile,
            offer_vip_time_status: res.data.offer_vip_time_status,
            is_ver: res.data.data_verification,
            userInfo:res.data
          })
        }
      })
    }
  },

  //电话联系
  contact: function (e) {
    let phone = this.data.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  contactver: function (e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  //跳转到注册页面
  registered: function (e) {
    if(this.data.is_ver == 0)
    {
      wx.navigateTo({
        url: '../my/registered?order_type=2',
      })
    }
    else
    {
      wx.navigateTo({
        url: '../my/myInfo',
      })
    }
  },
  //跳转到续费页面
  renew: function () {
    wx.navigateTo({
      url: '../my/myInfo',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})